package com.opcua.client.mvc.events;
import javafx.beans.Observable;
import javafx.css.PseudoClass;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.util.Callback;
public class MonitoredEventRowFactory<T extends MonitoredEvent> implements Callback<TableView<T>, TableRow<T>> {
  private final static PseudoClass BAD_CLASS = PseudoClass.getPseudoClass("bad");
  private final Callback<TableView<T>, TableRow<T>> callbackFactory;
  public MonitoredEventRowFactory(Callback<TableView<T>, TableRow<T>> callbackFactory) {
    this.callbackFactory = callbackFactory;
  }
  public MonitoredEventRowFactory() {
    this(null);
  }
  @Override
  public TableRow<T> call(TableView<T> tableView) {
    TableRow<T> row = callbackFactory != null ? callbackFactory.call(tableView) : new TableRow<>();
    row.itemProperty().addListener((Observable observable) -> updateRowStyle(row));
    return row;
  }
  private void updateRowStyle(TableRow<T> row) {
    if (row.getItem() == null) {
      return;
    }
    row.pseudoClassStateChanged(BAD_CLASS, row.getItem().getLastError() != null);
    row.getItem().lasterrorProperty().addListener(l -> row.pseudoClassStateChanged(BAD_CLASS, row.getItem().getLastError() != null));
  }
}
